<template>
  <div>
    <el-dialog
      title="分配功能"
      :closeOnClickModal="false"
      :visible.sync="show"
      width="540px">
      <el-form 
        label-position="right" 
        :model="selectTable" 
        ref="addForm" 
        label-width=""
        autocomplete="off"
        size="medium">
        <el-form-item label="角色:">
          {{this.selectTable.roleName}}
        </el-form-item>
        <el-scrollbar class="scroller" wrapClass="scrollbar-wrapper" style="height: 300px;">
          <el-tree
            ref="tree"
            :data="funcData"
            default-expand-all
            :check-strictly="true"
            :expand-on-click-node="false"
            show-checkbox
            node-key="functionId"
            :default-checked-keys="funcDataEd"
            @check-change="checkChange"
            :props="{
              children: 'children',
              label: 'functionName'
            }">
              <span class="custom-tree-node" slot-scope="{ node, data }" @click="checkClick(node)">
                <span>{{ node.label }}</span>
                <span>{{data.functionType===1?data.functionUri:data.functionCode}}</span>
              </span>
          </el-tree>
        </el-scrollbar>
      </el-form>
      <div slot="footer" class="dialog-footer" v-loading="pload">
        <el-button @click="show = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {roleFuncLists,roleSetFunc} from '@/utils/api'
export default {
  components: {
  },
  props: {
    data: Object
  },
  created () {
    this.selectTable =  this.JSONs(this.data)
    this.roleId = this.data.roleId
    this.getList();
  },
  watch: {
    show (newVal) {
      this.$parent.roleAssignPower = false;
    }
  },
  computed: {
  },
  data () {
    return {
      pload: false,
      rule: [
        { required: true, message: '请选择一个选项', trigger: 'blur' },
        { required: true, message: '必填项', trigger: 'blur' }
      ],
      show: true,
      selectTable: {},
      funcData: [],
      funcDataEd: [],
      filterMethod(query, item) {
        return item.functionName.indexOf(query) > -1;
      }
    }
  },
  methods: {
    checkChange (data,b) {
      if (b) {
        //自己是选中状态
        this.$refs.tree.setChecked(data.parentFunctionId,true)
      } else {
        //自己是非选中状态， 清除子节点选中
        data.children.map(item => {
          this.$refs.tree.setChecked(item.functionId,false)
        })
      }
    },
    checkClick (data) {
      if(!data.checked) {
        this.$refs.tree.setChecked(data.parentFunctionId,true)
      }
      this.$refs.tree.setChecked(data,!data.checked)
    },
    async getList () {
      let datas = await roleFuncLists({roleId: this.roleId});
      this.funcData = datas.allFunctionTree[0].children;
      this.funcDataEd = datas.assigned;
    },
    async submitForm(formName) {
      await this.$refs[formName].validate();
      try {
        this.pload = true
        let ids = this.$refs.tree.getCheckedKeys().join(',');
        await roleSetFunc({roleId:this.roleId,functionIds:ids});
        this.$notify({title: '操作成功', type: 'success', duration: 5000})
        this.$parent.roleAssignPower = false;
        this.$emit('getData');
      } catch(e) {
        console.log(e)
        this.pload = false
      }
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.custom-tree-node {
  flex:1;
  display: flex;
  justify-content: space-between;
  span:nth-child(2){
    font-size: 12px;
    color: #97a8be;
    padding-right: 15px;
  }
}
</style>